package com.example.coffeeseller_2.utils;

import android.content.Context;
import android.graphics.Bitmap;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.views.HomeActivity;

/**
 * Created by WH on 2018/7/26.
 * Glide加载图片时的处理
 */

public class GlideUtil {

    public void getBitmap(Context context, String url, final GlideBitmapListener glideBitmapListener) {

        SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

                glideBitmapListener.getBitmap(resource);
            }
        };
        if (context==null){
            return;
        }

        Glide.with(HomeActivity.getInstance())

                .load(url)

                .asBitmap()

                .placeholder(R.mipmap.no_coffee)

                //禁止内存缓存
                .skipMemoryCache(true)
                //禁止磁盘缓存（Glide默认缓存策略是：DiskCacheStrategy.RESULT）
                .diskCacheStrategy(DiskCacheStrategy.NONE)

//                .override(200, 200)

                .dontAnimate()

                .into(simpleTarget);

    }

    public interface GlideBitmapListener {
        void getBitmap(Bitmap bitmap);
    }
}
